package com.xu.wemall.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xu.wemall.entry.Player;
import com.xu.wemall.service.IPlayerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "球员操作接口")
@RestController
@RequestMapping("/player")
public class PlayerController {

    @Autowired
    private IPlayerService iPlayerService;

    @ApiOperation("导出Excel")
    @PostMapping("/exportExcel")
    public void ExportExcel() {
        // 设置文件导出的路径
        String realPath = "D://Excel/";
        File folder = new File(realPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String fileName = realPath + "Player" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为用户表 然后文件流会自动关闭
        EasyExcel.write(fileName, Player.class).sheet("用户表").doWrite(iPlayerService.list());
    }

    @ApiOperation("导入Excel")
    @PostMapping("/importExcel")
    public void ImportExcel(@RequestPart("file") MultipartFile multipartFile) throws IOException {
        if (multipartFile.isEmpty()) {
            return;
        }
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        EasyExcel.read(multipartFile.getInputStream(), Player.class,
                new PageReadListener<Player>(dataList -> {
            //将导入的数据用mybatisPlus一个个添加进数据库
            iPlayerService.saveBatch(dataList);
        })).sheet().doRead();
    }

    public List<Player> getDataList() {
        final int pageSize = 10;
        int pageNum = 1;

        List<Player> dataList = new ArrayList<>();
        IPage<Player> iPage = new Page<>(pageNum, pageSize);
        do {
            iPage.setCurrent(pageNum);
            iPage= iPlayerService.page(iPage);
            if (CollectionUtils.isEmpty(iPage.getRecords())){
                break;
            }
            dataList.addAll(iPage.getRecords());
            pageNum++;
        } while (!CollectionUtils.isEmpty(iPage.getRecords()));
        return dataList;
    }

    @ApiOperation("分页查询数据")
    @GetMapping("/getPageList")
    public IPage<Player> getPageList() {
        Page<Player> iPage = new Page<>(1, 10);
        QueryWrapper<Player> queryWrapper = new QueryWrapper<>();
        IPage<Player> userRecord = iPlayerService.page(iPage, queryWrapper);
        userRecord.getRecords().forEach(System.out::println);
        return userRecord;
    }

    @ApiOperation("查询所有数据")
    @GetMapping("/getAllList")
    public List<Player> getAllList() {
        final int pageSize = 10;
        int pageNum = 1;

        List<Player> dataList = new ArrayList<>();
        IPage<Player> iPage = new Page<>(pageNum, pageSize);
        do {
            iPage.setCurrent(pageNum);
            iPage= iPlayerService.page(iPage);
            if (CollectionUtils.isEmpty(iPage.getRecords())){
                break;
            }
            dataList.addAll(iPage.getRecords());
            pageNum++;
        } while (!CollectionUtils.isEmpty(iPage.getRecords()));
        return dataList;
    }

}
